/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Factory;

import domains.Users;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author hxd
 */
public class DbService {
    public static Session session;
    public static void openSession(){
        session = HibernateUtil.getSessionFactory().openSession();
    }
    public static List select(String hql) {
        try {
            //Session session = HibernateUtil.getSessionFactory().openSession();
            //session.beginTransaction();
            Query q = session.createQuery(hql);
            List resultList = q.list();
            // displayResult(resultList);
            //session.getTransaction().commit();
            return resultList;
        } catch (HibernateException he) {
            he.printStackTrace();
        }
        return null;
    }

    public static boolean insert(Object obj) {
        try {
           // Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(obj);
            // Query q = session.createQuery(hql);
            // List resultList = q.list();
            // displayResult(resultList);
            session.getTransaction().commit();
            return true;
        } catch (HibernateException he) {
            he.printStackTrace();
            return false;
        }
        // return null;

    }

    public static boolean update(Object obj) {
        try {
           // Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(obj);
             
            // Query q = session.createQuery(hql);
            // List resultList = q.list();
            // displayResult(resultList);
            session.getTransaction().commit();
            return true;
        } catch (HibernateException he) {
            he.printStackTrace();
            return false;
        }
        // return null;

    }

    public static boolean delete(Object obj) {
        try {
           // Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(obj);
            // Query q = session.createQuery(hql);
            // List resultList = q.list();
            // displayResult(resultList);
            session.getTransaction().commit();
            return true;
        } catch (HibernateException he) {
            he.printStackTrace();
            return false;
        }
        // return null;

    }

    public static Users getUser(String loginName) {
        List<Users> list = Factory.DbService.select("from Users where loginName='" + loginName+"'");
        //System.out.println(list.size());
        if(list.size()>0){
            Users user = list.get(0);
       // System.out.println(user.getName() + user.getPwd());
            return user;
        }else
            return null;
    }
    public static Users getUser(Integer userId){
        // Session session = HibernateUtil.getSessionFactory().openSession();
         session.close();
         return (Users)session.get(Users.class, userId); 
    }
    public static void closeSession(){
        session.close();
        session=null;
    }
}
